package com.cdsf.kennedy.user.service.impl;

import com.cdsf.common.conf.AbstractService;
import com.cdsf.common.util.Page;
import com.cdsf.kennedy.user.domain.User;
import com.cdsf.kennedy.user.mapper.UserMapper;
import com.cdsf.kennedy.user.service.UserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.weekend.Weekend;
import tk.mybatis.mapper.weekend.WeekendCriteria;

import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @Author: kennedy
 * @Date: Created in 15:55 2018/2/6
 */
@Service
public class UserServiceImpl extends AbstractService implements UserService {
    @Autowired
    private UserMapper userMapper;


    @Override
    public void insert(User user) {
        user.setUuid(randomUUID());
        user.setCreateTime(new Date());
        userMapper.insert(user);
    }

    @Override
    public User selectOne(User user) {
        User result = userMapper.selectOne(user);
        return result;
    }

    public List<User> getAll(User user, Page page) {
        PageHelper.startPage(page.getPage(), page.getRows());
        Weekend<User> weekend = Weekend.of(User.class);
        WeekendCriteria<User, Object> criteria = weekend.weekendCriteria();
        if (user.getUserName() != null) {
            criteria.andLike(User::getUserName, "%" + user.getUserName() + "%");
        }
        return userMapper.selectByExample(weekend);
    }

    @Override
    public void delete(String uuid) {
        userMapper.deleteByPrimaryKey(uuid);
    }

    @Override
    public User selectByPrimaryKey(String uuid) {
        return userMapper.selectByPrimaryKey(uuid);
    }

    @Override
    public void updateByPrimaryKey(User user) {
        userMapper.updateByPrimaryKey(user);
    }

    @Override
    public void timeOut(int i) {
        try {
            TimeUnit.SECONDS.sleep(i);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    @Override
    public PageInfo<User> getAll_(User user, Page page) {
        PageHelper.startPage(page.getPage(), page.getRows());
        List<User> users=userMapper.getList(user);
        return new PageInfo(users);
    }
}
